<script setup lang="ts">
import Todolist from "./components/Todolist.vue";
import Todofilter from "./components/Todofilter.vue";
import Todoadd from "./components/Todoadd.vue";
import { Todo } from "./type/globel";
import { ref, reactive, computed } from "vue";
const todos = reactive<Array<Todo>>([]);
const addTodo = (todo: any) => {
  todos.push(todo);
};

const selected = ref("all");
// 基于原数据包装一层
const filterTodo = computed(() => {
  switch (selected.value) {
    case "done":
      return todos.filter((todo) => todo.completed);
    case "todo":
      return todos.filter((todo) => !todo.completed);
    default:
      return todos;
  }
});
</script>

<template>
  <div class="main">
    <div class="container">
      <Todoadd :tid="todos.length" @addTodo="addTodo"></Todoadd>
      <Todofilter
        :selected="selected"
        @change-filter="selected = $event"
      ></Todofilter>
      <Todolist :todos="filterTodo"></Todolist>
    </div>
  </div>
</template>

<style>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: Helvetica, "PingFang SC", "Microsoft Yahei", sans-serif;
}

/* 整个页面 */
.main {
  width: 100vw;
  min-height: 100vh;
  display: grid;
  align-items: center;
  justify-items: center;
  background: rgb(216, 243, 214);
}

.container {
  width: 60%;
  max-width: 400px;
  box-shadow: 0px 0px 24px rgba(26, 25, 25, 0.15);
  border-radius: 24px;
  padding: 48px 28px;
  background-color: rgb(229, 230, 235);
}

/* 标题 */
h1 {
  margin: 24px 0;
  font-size: 28px;
  color: #384280;
}

/* 添加框 */
.add-todo {
  position: relative;
  display: flex;
  align-items: center;
}

.add-todo input {
  padding: 16px 52px 16px 18px;
  border-radius: 48px;
  border: none;
  outline: none;
  box-shadow: 0px 0px 24px rgba(0, 0, 0, 0.08);
  width: 100%;
  font-size: 16px;
  color: #626262;
}

.add-todo button {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: linear-gradient(#b6f1a2, #6df86d);
  border: none;
  outline: none;

  color: white;
  position: absolute;
  right: 0px;

  cursor: pointer;
}

.add-todo .add-icon {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(#fff, #fff), linear-gradient(#fff, #fff);
  background-size: 50% 2px, 2px 50%;
  background-position: center;
  background-repeat: no-repeat;
}

/* 过滤选项 */
.filters {
  display: flex;
  margin: 24px 2px;
  color: #c0c2ce;
  font-size: 14px;
}

.filters .filter {
  margin-right: 14px;
  transition: 0.8s;

  /* 鼠标移动过去时，成为小手 */
  cursor: pointer;
}

.filters .filter.active {
  color: #6b729c;
  transform: scale(1.2);
}

/* todo 列表 */
.todo-list {
  display: grid;
  row-gap: 14px;
}

.todo-item {
  background: white;
  padding: 16px;
  border-radius: 8px;
  color: #626262;
}

.todo-item label {
  position: relative;
  display: flex;
  align-items: center;
}

.todo-item label span.check-button {
  position: absolute;
  top: 0;
}

.todo-item label span.check-button::before,
.todo-item label span.check-button::after {
  content: "";
  display: block;
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
}

.todo-item label span.check-button::before {
  border: 1px solid #7ad86d;
}

.todo-item label span.check-button::after {
  transition: 0.4s;
  background: #7ad86d;
  transform: translate(1px, 1px) scale(0.8);
  opacity: 0;
}

.todo-item input {
  margin-right: 16px;
  opacity: 0;
}

.todo-item input:checked + span.check-button::after {
  opacity: 1;
}
/* 选中后删除线 */
.todo-item.done label {
  /* 划线 */
  text-decoration: line-through;
  /* 斜体 */
  font-style: italic;
}
</style>

